# Copyright 2021 hpmicro
# SPDX-License-Identifier: BSD-3-Clause

cmake_minimum_required(VERSION 3.13)

#set(USE_USBHOST_LINKER_FILE 1)

# set(HPM_BOOTUSER_OTA1 1)
set(HPM_BOOTUSER_OTA2 1)

# set(HPM_OTA_ENET_MODE 1)
set(HPM_OTA_UART_MODE 1)
# set(HPM_OTA_USB_MODE  1)

set(HPM_SDK_NO_NANO_SPECS 1)

if (${USE_USBHOST_LINKER_FILE})
if (${HPM_BOOTUSER_OTA1})
    set(CUSTOM_GCC_LINKER_FILE ../usb_host_msc_ota/linkers/segger/user_app_ota1.icf)
endif()
if (${HPM_BOOTUSER_OTA2})
    set(CUSTOM_GCC_LINKER_FILE D:/cusfile/SDK/sdk/sdk_env_v1.5.0/hpm_sdk/samples/ota/linkers/segger/user_app_ota2.icf)
endif()						   
else()
if (${HPM_BOOTUSER_OTA1})
    set(CUSTOM_GCC_LINKER_FILE ../linkers/segger/user_app_ota1.icf)
endif()
if (${HPM_BOOTUSER_OTA2})
    set(CUSTOM_GCC_LINKER_FILE D:/cusfile/SDK/sdk/sdk_env_v1.5.0/hpm_sdk/samples/ota/linkers/segger/user_app_ota2.icf)
endif()
endif()

if (${HPM_OTA_ENET_MODE})
    set(CONFIG_LWIP 1)
    set(CONFIG_ENET_PHY_DP83867 1)
    # set(CONFIG_ENET_PHY_RTL8211 1)
    # set(CONFIG_ENET_PHY_DP83848 1)
    # set(CONFIG_ENET_PHY_RTL8201 1)
endif()

if (${HPM_OTA_USB_MODE})
    set(CONFIG_CHERRYUSB 1)
    set(CONFIG_USB_DEVICE 1)
    set(CONFIG_CHERRYUSB_DEVICE 1)
    set(CONFIG_CHERRYUSB_DEVICE_MSC 1)
endif()

find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})

# OTA .....
if(${HPM_BOOTUSER_OTA1})
    sdk_compile_definitions(-DBOOTUSER_OTA1=1)
endif()

if(${HPM_BOOTUSER_OTA2})
    sdk_compile_definitions(-DBOOTUSER_OTA2=1)
endif()
#ENET ........
if (${HPM_OTA_ENET_MODE})
    sdk_compile_definitions(-DUSER_OTA_MODE_ENET=1)
    if(${CONFIG_ENET_PHY_DP83867})
    sdk_compile_definitions(-DRGMII=1)
    sdk_compile_definitions(-D__USE_DP83867=1)
    endif()

    if(${CONFIG_ENET_PHY_RTL8211})
    sdk_compile_definitions(-DRGMII=1)
    sdk_compile_definitions(-D__USE_RTL8211=1)
    endif()

    if(${CONFIG_ENET_PHY_DP83848})
    sdk_compile_definitions(-DRGMII=0)
    sdk_compile_definitions(-D__USE_DP83864=1)
    endif()

    if(${CONFIG_ENET_PHY_RTL8201})
    sdk_compile_definitions(-DRGMII=0)
    sdk_compile_definitions(-D__USE_RTL8201=1)
    endif()
endif()
#UART ....
if (${HPM_OTA_UART_MODE})
    sdk_compile_definitions(-DUSER_OTA_MODE_UART=1)
endif()

#USB ....
if (${HPM_OTA_USB_MODE})
    sdk_compile_definitions(-DUSER_OTA_MODE_USB=1)
endif()


sdk_compile_definitions(-DFLASH_XIP=1)
sdk_compile_definitions(-DINIT_EXT_RAM_FOR_DATA=1)
sdk_compile_definitions(-DBOARD_SHOW_CLOCK=0)

project(user_app)
sdk_inc(../common)

if (${HPM_OTA_ENET_MODE})
sdk_inc(../enet_ota/inc)
sdk_inc(../enet_ota/inc/arch)
sdk_inc(../enet_ota/inc/eth)
sdk_inc(../enet_ota/inc/app)
sdk_app_src(../enet_ota/src/arch/ethernetif.c)
sdk_app_src(../enet_ota/src/arch/sys_arch.c)
sdk_app_src(../enet_ota/src/eth/netconf.c)
sdk_app_src(../enet_ota/src/app/tcp_echo.c)
sdk_app_src(../enet_ota/src/lwip.c)
endif()

if (${HPM_OTA_UART_MODE})
sdk_inc(../uart_ota/inc)
sdk_app_src(../uart_ota/src/uart_xmodem.c)
endif()

if (${HPM_OTA_USB_MODE})
sdk_inc(../usb_device_msc_ota/inc)
sdk_app_src(../usb_device_msc_ota/src/msc_ram.c)
endif()

sdk_app_src(../common/boot_user.c)
sdk_app_src(../common/ota_api.c)
sdk_app_src(../common/user_signature.c)
sdk_app_src(../common/ota_verify.c)
sdk_app_src(src/hello_world.c)
generate_ses_project()
